template.defaults.imports.TimestampToTime = function (timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes() + ':';
    var s = date.getSeconds();
    return Y + M + D + h + m + s;
};

    /* global getCookie */
    var loading = false; //状态标记
    var hasNextPage = false;
    var pageNo = 1;
    var userInfo = {};
    var orderState = -1;//1全部 2待审核 3待售卖
    

    $(function () {
        $.get('/antique/api/sysUser/query_my_info', { token:getCookie('token') }, function (result) {
            if (result.code == 0) {
                var data = result.data;
                if (data) {
                    userInfo = data;
                    queryList(pageNo);
                }
            }
        });
    
    });

    //下拉刷新
    $("#aroundBox").pullToRefresh().on("pull-to-refresh", function () {
        $("#contentTo").html("");
        pageNo = 1;
        queryList(pageNo,function(){
            $("#aroundBox").pullToRefreshDone(); // 重置下拉刷新
        });
    });

    //上拉加载更多
    $('#aroundBox').infinite().on("infinite", function () {
        if (hasNextPage){
            pageNo++;
            queryList(pageNo,function(){
            },true);
        }        
    });

    function queryList(pageNo,callback,isMore){
        if (loading) return;
        loading = true;
        var params = {};
        params.start = (pageNo - 1) * 20;
        params.limit = 20;
        params.userId = userInfo.id;
        params.bidType = 1;// 1竞价 2议价
        params.token = getCookie('token');
        params.state = orderState;
        if (orderState == -1) {
            delete params.state;
        } 
        $.get('/antique/api/productBidLog/query_list', params, function (result) {
            if (result.code == 0) {
                if (!isMore){
                    /* global template */
                    document.getElementById('contentTo').innerHTML = template('content', result.data);                
                } else {
                    $("#contentTo").append(template('content', result.data));
                }
                hasNextPage = result.data.hasNextPage;
                if (!result.data.hasNextPage) {
                    $('#loadmore').hide();
                    $('#nomore').show();
                } else {
                    $('#loadmore').show();
                    $('#nomore').hide(); 
                }
                if (callback) callback();
                loading = false;
            }

            
        });
    }

$(".weui-tab .tabs .item").click(function () {
    $(".weui-tab .tabs .item").removeClass("current");
    $(this).addClass("current");

    orderState = $(this).data('state');
    $("#contentTo").html("");
    pageNo = 1;
    queryList(pageNo,function(){
        $("#aroundBox").pullToRefreshDone(); // 重置下拉刷新
    });
    
});


function agentPic (rows) {//productId
    var row = JSON.parse(rows);
    $('.nowPrice').text('￥' + row.productDetail.nowPrice);
    $('.bidIncrementPrice').text('￥' + row.productDetail.bidIncrementPrice );
    $("#confirmAgentPrice").attr('data-productid',row.productDetail.id);
    var agentSendPrice = $('#agentSendPrice');
    agentSendPrice.empty();
    var currentPrice = parseInt(row.productDetail.nowPrice) + parseInt(row.productDetail.bidIncrementPrice);
    agentSendPrice.append('<option selected="" value="' + currentPrice + '">￥' + currentPrice + '</option>');
    for (var i = 0;i < 100; i++){
        currentPrice = currentPrice + parseInt(row.productDetail.bidIncrementPrice);
        agentSendPrice.append('<option value="' + currentPrice + '">￥' + currentPrice + '</option>');
    }
    $("#agent_price").popup();
   
    
}
$('#confirmAgentPrice').click(function(){
    var agentSendPrice = parseInt($('#agentSendPrice').val());
    var productId = $(this).data('productid');
    /* global isInteger */
    if (agentSendPrice && isInteger(agentSendPrice)){
        $.post('/antique/api/productBidTrusteeship/trusteeship_bid', { productId:productId,token:getCookie('token'),maxPrice:agentSendPrice }, function (result) {
            if (result.code == 0) {
                $.toast("操作成功！");
                $("#contentTo").html("");
                pageNo = 1;
                queryList(pageNo,function(){},true);
            } else {
             $.toast(result.message, "cancel");
            }
         });         
    } else {
        $.toast("必须输入数字", "forbidden");
    }        
});
